#include<iostream>
#include<string>
#include<unordered_map>
#include<unordered_set>
using namespace std;
const int N = 5e5 + 10;
int dp[N][27];
bool sub_two[27][27];
string s, t;
bool cset[27];
int main()
{
    cin >> s >> t;
    for (int i = 0; i < t.size(); i++)
    {
        for (int j = 0; j < 26; j++)
        {
            if (cset[j] == true)
            {
                sub_two[j][t[i] - 'a'] = true;
            }
        }
        cset[t[i] - 'a'] = true;
    }
    dp[0][s[0] - 'a'] = 1;
    for (int i = 1; i < s.size(); i++)
    {
        dp[i][s[i] - 'a'] = 1;
        for (int j = 0; j < 26; j++)
        {
            dp[i][j] = max(dp[i][j], dp[i - 1][j] + (cset[s[i] - 'a'] == false));
        }
        if (cset[s[i] - 'a'] == false) continue;
        for (int j = 0; j < 26; j++)
        {
            if (sub_two[j][s[i] - 'a'] == false)
            {
                dp[i][s[i] - 'a'] = max(dp[i][s[i] - 'a'], dp[i - 1][j] + 1);
            }
        }
    }
    int ans = 0;
    for (int i = 0; i < 26; i++) ans = max(ans, dp[s.size() - 1][i]);
    cout << ans << endl;
    return 0;
}